package com.ctg.payroll2.util;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * @author sami
 * 
 */
public class FormartVariable extends SqlMapClientDaoSupport {

	public static double toDouble(String inStr) throws Exception {
		if (inStr == null || "".equals(inStr)) {
			return 0;
		} else {
			return Double.valueOf(inStr).doubleValue();
		}
	}

	public static int toInt(String inStr) throws Exception {
		if (inStr == null || "".equals(inStr)) {
			return 0;
		} else {
			return Integer.valueOf(inStr).intValue();
		}
	}

	public static BigDecimal getBigDecimal(String str) {
		BigDecimal bd = null;
		if (str == null)
			return null;
		try {
			bd = new BigDecimal(str);
		} catch (Exception e) {
			return null;
		}
		return bd;
	}

	public static String toStr(String str) {
		if (str == null)
			return "";
		if (str.equals("null")) {
			return "";
		} else {
			return str.replaceAll("'", "");
		}
	}

	/**
	 * 该方法暂时无法使用,请勿用
	 * 
	 * @param sequenceName
	 * @return
	 * @throws Exception
	 */
	public static String getNewId(String sequenceName) throws Exception {
		Connection connection = null;
		String sql = "select " + sequenceName + ".nextval from dual";
		String newId = "";
		if (connection != null)
			try {
				Statement st = connection.createStatement();
				ResultSet rs = st.executeQuery(sql);
				if (rs.next()) {
					newId = rs.getString(1);
				}
				rs.close();
				st.close();
//				System.out.println("*****************:" + newId);
			} finally {
				connection.close();
			}
		else
			return null;
		return newId;
	}

}
